package com.apes.fn.fico.expense.platformFee.process;

import com.apes.fn.business.insurance.modle.PolicyInformation;
import com.apes.fn.business.insurance.modle.PolicyInformationItem;
import com.apes.fn.business.insurance.repository.PolicyInformationRepository;
import com.apes.fn.fico.expense.platformFee.CommonFeeProcess;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 车险保单生成平台费用细项单据
 * 同一单据可能满足多种条件，无法走公共方法 CommonFeeProcess 中的 createExpense 生成
 */
@Service
public class PolicyInformationFeeProcess extends CommonFeeProcess {

    @Autowired
    private PolicyInformationRepository policyInformationRepository;

    @Override
    public void process(SimpleRequest request) {
        String id = request.get("id");
        PolicyInformation policyInformation = policyInformationRepository.findOne(id);
        createExpense(MapUtil.mapper("order", policyInformation));
    }

    /**
     * 平台服务费
     *
     * @param map {"order": PolicyInformation}
     */
    @Override
    public Map<String, Object> getExpenseModel(Map map) {
        PolicyInformation policyInformation = (PolicyInformation) map.get("order");
        double fnCommission = policyInformation.getPolicyInformationItems().stream().mapToDouble(PolicyInformationItem::getFnCommission).sum();
        double platformFreeRatio = getPlatformFreeRatio(policyInformation.getCompany().getId());
        double expenseAmount = fnCommission * platformFreeRatio;
        return MapUtil.mapper("order", policyInformation.getId(), "orderType", "PolicyInformation",
                "source", policyInformation.getId(), "sourceBusinessType", "policyInformation",
                "summary", "管理服务费", "incomeTypeId", "A09", "expenseDetailsTypeId", "002052",
                "paymentAmount", fnCommission, "expenseAmount", expenseAmount,"date",policyInformation.getCreateDate(),
                "deptId", policyInformation.getDept().getId(), "companyId", policyInformation.getCompany().getId());
    }

    @Override
    public boolean support(String type) {
        return "PolicyInformation".equals(type);
    }
}
